object FunctionParametersDemo extends App {
  // 1. 默认参数：统计成绩时，默认按满分100分计算及格线
  def calculatePassRate(scores: List[Int], passLine: Int = 60): Double = {
    if (scores.isEmpty) 0.0
    else {
      val passCount = scores.count(_ >= passLine)
      passCount.toDouble / scores.size * 100
    }
  }

  // 2. 命名参数：当参数较多时，使用命名参数使代码更清晰
  def generateScoreReport(
                           className: String,
                           subject: String,
                           totalStudents: Int,
                           averageScore: Double,
                           passRate: Double
                         ): String = {
    s"""|===== 成绩报告 =====
        |班级：$className
        |科目：$subject
        |学生总数：$totalStudents
        |平均分：$averageScore
        |及格率：$passRate%
        |===================""".stripMargin
  }

  // 3. 可变参数：处理数量不固定的参数
  def calculateTotalScore(scores: Int*): Int = {
    scores.sum
  }

  // 4. 混合使用不同参数形式
  def analyzeScores(
                     className: String,
                     subject: String,
                     scores: Int*
                   ): Unit = {
    if (scores.isEmpty) {
      println(s"$className 的 $subject 没有成绩数据")
      return
    }
    val total = calculateTotalScore(scores: _*)
    val average = total.toDouble / scores.size
    val passRate = calculatePassRate(scores.toList, passLine = 60)
    println(generateScoreReport(
      className = className,
      subject = subject,
      totalStudents = scores.size,
      averageScore = average,
      passRate = passRate
    ))
  }

  // 调用函数
  val classMathScores = List(85, 92, 58, 76, 88, 90, 65, 72, 45, 80)

  // 使用默认参数
  val defaultPassRate = calculatePassRate(classMathScores)
  println(s"默认及格线(60分)的及格率：$defaultPassRate%")

  // 覆盖默认参数
  val strictPassRate = calculatePassRate(classMathScores, 70)
  println(s"严格及格线(70分)的及格率：$strictPassRate%")

  // 使用可变参数
  val total = calculateTotalScore(85, 92, 76, 88)
  println(s"4名学生的总分：$total")

  // 使用命名参数和默认参数
  println("\n生成成绩报告：")
  analyzeScores(
    className = "高一(1)班",
    subject = "数学",  // 修正了"数字"为"数学"
    scores = 85, 92, 58, 76, 88, 90, 65, 72, 45, 80
  )
}